import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/prescription_api.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AddPrescriptionPage extends StatefulWidget {
  final int patientID;

  AddPrescriptionPage({Key key, @required this.patientID}) : super(key: key);

  @override
  _AddPrescriptionPageState createState() => _AddPrescriptionPageState();
}

class _AddPrescriptionPageState extends State<AddPrescriptionPage> {
  TextEditingController decriptionController = TextEditingController();
  TextEditingController medicineController = TextEditingController();
  final TextStyle textStyle = TextStyle(
    color: Colors.grey,
    fontSize: 20,
  );

  Widget genarateInput({
    @required String title,
    @required String placeholder,
    @required TextEditingController controller,
  }) {
    return ListView(
      shrinkWrap: true,
      physics: new NeverScrollableScrollPhysics(),
      children: <Widget>[
        SizedBox(height: 20),
        Container(
          margin: EdgeInsets.only(left: 10),
          child: Text(title, style: textStyle),
        ),
        SizedBox(height: 20),
        TextField(
          controller: controller,
          keyboardType: TextInputType.multiline,
          decoration: InputDecoration(
            hintText: placeholder,
            border: InputBorder.none,
            fillColor: Colors.white,
            filled: true,
          ),
          maxLength: 200,
          minLines: 6,
          maxLines: 8,
        ),
      ],
    );
  }

  // 增加处方
  _submit() async {
    dynamic res = await addPrescription_api({
      'patientID': widget.patientID,
      'description': decriptionController.text,
      'medicine': medicineController.text
    });
    res['code'] == 0 ? successToast('增加成功') : errorToast(res['message']);
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[200],
      appBar: AppBar(
        actions: <Widget>[
          Flex(
            direction: Axis.vertical,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(right: 10),
                child: GestureDetector(
                  onTap: _submit,
                  child: Text(
                    '提交',
                    style: TextStyle(fontSize: 20),
                  ),
                ),
              ),
            ],
          ),
        ],
        title: Text('增加处方'),
        centerTitle: true,
      ),
      body: ListView(
        children: <Widget>[
          genarateInput(
            title: '病情分析',
            controller: decriptionController,
            placeholder: '请输入你的病情分析',
          ),
          genarateInput(
            title: '药物推荐',
            controller: medicineController,
            placeholder: '药物推荐',
          ),
        ],
      ),
    );
  }
}
